package com.colin.config;

import cn.hutool.core.io.FileUtil;
import cn.hutool.json.JSONUtil;
import com.colin.consumer.PathConsumer;
import com.colin.core.PathStorage;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.amqp.RabbitAutoConfiguration;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnSingleCandidate;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Conditional;
import org.springframework.context.annotation.Configuration;

import java.io.FileOutputStream;
import java.nio.charset.StandardCharsets;
import java.nio.file.Path;
import java.util.HashSet;
import java.util.Set;

@Configuration
@EnableConfigurationProperties(PathStorageProperties.class)
@ConditionalOnClass(ConnectionFactory.class)
@ConditionalOnSingleCandidate(ConnectionFactory.class)
@AutoConfigureAfter(RabbitAutoConfiguration.class)
public class PathStorageAutoConfig implements InitializingBean, DisposableBean {

    @Autowired
    private PathStorageProperties pathStorageProperties;

    @Bean
    public Set<String> pathSet() {
        System.out.println(pathStorageProperties);
        return pathStorageProperties.getPathSet();
    }

    @Bean
    public PathConsumer pathConsumer() {
        PathConsumer pathConsumer = new PathConsumer();
        pathConsumer.setPathStorageProperties(this.pathStorageProperties);
        return pathConsumer;
    }

    /**
     * 这个方法会在当前bean创建时执行 对于单例bean这个方法的执行时机其实就是服务进程启动时 (启动完成之前)
     * @throws Exception
     */
    @Override
    public void afterPropertiesSet() throws Exception {
        // 读取网关服务线下前 持久化的set集合中的数据
        String pathLog = pathStorageProperties.getPathLog();

        String jsonStr = FileUtil.readString(pathLog, StandardCharsets.UTF_8);
        PathStorage pathStorage = JSONUtil.toBean(jsonStr, PathStorage.class);
        if (pathStorage == null) {
            pathStorage = new PathStorage();

        }

        if (pathStorage.getPathSet() == null) {
            pathStorage.setPathSet(new HashSet<>());
        }

        pathStorageProperties.setPathSet(pathStorage.getPathSet());



        System.out.println("当前从文件中读取到的接口路径 ： " + pathStorageProperties.getPathSet());
    }

    /**
     * 这个方法会在当前bean销毁时执行 对于单例bean这个方法的执行时机其实就是服务进程关停时
     * @throws Exception
     */
    @Override
    public void destroy() throws Exception {
        // 将当前set集合中的数据写入文件
        Set<String> pathSet = pathStorageProperties.getPathSet();
        String pathLog = pathStorageProperties.getPathLog();

        PathStorage pathStorage = new PathStorage();
        pathStorage.setPathSet(pathSet);


        String jsonStr = JSONUtil.toJsonStr(pathStorage);


        System.out.println("即将写入文件存储的接口路径 : " + jsonStr);
        // 思考 这个工具文件不存在时 创建文件写入内容 文件已存在并且有内容时 是覆盖还是拼接
        FileUtil.writeString(jsonStr, pathLog, StandardCharsets.UTF_8);
    }
}
